package com.atguigu.jxc.controller;

import com.atguigu.jxc.entity.GoodsCount;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.ReturnList;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("returnListGoods")
@Slf4j
public class ReturnListGoodsController {

    @Autowired
    private ReturnListGoodsService returnListGoodsService;

    /**
     * 保存退货单
     * @param returnList
     * @param returnListGoodsStr
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr){
        return returnListGoodsService.save(returnList,returnListGoodsStr);
    }

    /**
     * 查询退货单
     * @param returnNumber
     * @param supplierId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime){
        HashMap<String, Object> map = new HashMap<>();
        List<ReturnList> list = returnListGoodsService.list(returnNumber,supplierId,state,sTime,eTime);
        map.put("rows",list);
        return map;
    }

    /**
     * 删除退货单和退货单中的商品
     * @param returnListId
     * @return
     */
    @PostMapping("delete")
    public ServiceVO delete(Integer returnListId){
        returnListGoodsService.delete(returnListId);
        return new ServiceVO(100,"请求成功");
    }


    /**
     * 退货统计（可根据 商品类别、商品编码或名称 条件查询）
     * @return
     */
    @RequestMapping("count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        List<GoodsCount> list = returnListGoodsService.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String res = gson.toJson(list);
        log.info("--------退货统计--------");
        return res;
    }
}
